﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IronOne.SalesOne.BusinessObjects;
using Facade;
using System.Configuration;

namespace IronOne.SalesOne.SalesOneWeb.BatchJobs
{
    public partial class JobHistory : SOSecurityPage, IntTest, IDataPage
    {
        private int pageNo = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidateUserCredentials(Request.Url.LocalPath);
            
            if (!Page.IsPostBack)
            {
                pageNo = 1;
                lblHeader.Text = "History information : " + Request.QueryString["jobId"];

                try
                {
                    populateGrid(pageNo);
                }
                catch
                {
                    MassageSummery("IIS514");
                }
                UCGrid1.SetText("1");
            }
        }

        private void populateGrid(int pageNo)
        {
            string jobId = Request.QueryString["jobId"];

            ConfiguraionFacade config = new ConfiguraionFacade();
            int[] numbers = { 0, 1};
            
            List<IronOne.SalesOne.BusinessObjects.JobHistory> list = config.getHistoryForJob(jobId, pageNo);
            if (list.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
            {
                UCGrid1.IsLastPage = (config.getHistoryForJob(jobId, pageNo + 1)).Count > 0 ? false : true;
            }
            
            UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "StartTime",
                    ColumnHeader = "Start Time",
                    ColumnWidth = 120},
                new GridViewStyle{
                    ColumnId = "Duration",
                    ColumnHeader = "Duration",
                    ColumnWidth = 60,
                    Align = "right"},
                new GridViewStyle{
                    ColumnId = "Status",
                    ColumnHeader = "Status",
                    ColumnWidth = 300},
                new GridViewStyle{
                    ColumnId = "CompletionTime",
                    ColumnHeader = "Completion Time",
                    ColumnWidth = 120}
            };
                


            UCGrid1.SetDataSource(list, numbers);

            if (list.Count <= 0)
            {
                MassageSummery("IIS504");
            }
        }


        public void Next()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                UCGrid1.SetText(pageNo.ToString());

            }
            else
            {
                pageNo = 2;
                UCGrid1.SetText("2");

            }

            populateGrid(pageNo);
        }

        public void Previous()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                UCGrid1.SetText(pageNo.ToString());

                populateGrid(pageNo);
            }
        }

        public void Go()
        {
            int p = int.Parse(UCGrid1.GetText());
            if (p != 0)
            {
                try
                {
                    populateGrid(p);
                    pageNo = p;
                }
                catch (Exception ex)
                {
                    throw;
                }


            }

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/BatchJobs/JobScheduler.aspx");
        }

        #region IDataPage Members

        public void Create()
        {
            
        }

        public void View()
        {
            
        }

        public void Update()
        {
            
        }

        public void Delete()
        {
            
        }

        public void Cancel()
        {
            
        }

        public void GetId(GridViewRow row)
        {
            
        }

        #endregion
    }
}
